<?php
namespace app\home\model;

//加载配置类
use think\Config;
//Model
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class CartsModel extends Model
{
	/**
	*	加入购物车-商品信息
	*   @param array $data  数据
	*/
	public function addCart($data)
	{
		$map = [];
		if($data){
			/* 先添加下面字段到购物车表,总金额后面再加 */
			$map['uid'] = Session::get('user_id');
			$map['gid'] = $data['gid'];
			$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
			$res = Db::table('sz_cart')->insertGetId($map);
			
			return $res;

		}
	}

	/**
	*	加入购物车-商品规格数量
	*   @param array $data  数据
	*/

	public function addCartInfo($data)
	{
		if($data){
			$cate['tid'] = $data['tid'];
			$cate['gid'] = $data['gid'];
			$cate['iid'] = $data['iid'];
			$cate['ci_total'] = $data['i_price']*$data['i_number']*$data['ci_number'];
			$cate['ci_number'] = $data['ci_number'];
			Db::table('sz_cartinfo')->insert($cate);

			return true;

		}else{

			return false;
		}

	}

	/**
	*	购物车的总价格
	*/
	public function addCartsTotal($tid)
	{
		if($tid){
			$t_total = Db::table('sz_cartinfo')->where('tid',$tid)->sum('ci_total');
			
			/* 把总价格加入到购物车表里面 */
			Db::table('sz_cart')->where('id',$tid)->update(['t_total'=>$t_total]);

			return true;
		}else{
			return false;
		}
		
	}


	/**
	*	立即购买-商品信息
	*   @param array $data  数据
	*/
	public function addNows($data)
	{
		$map = [];
		if($data){
			/* 先添加下面字段到购物车表,总金额后面再加 */
			$map['uid'] = Session::get('user_id');
			$map['gid'] = $data['gid'];
			$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
			$res = Db::table('sz_cart')->insertGetId($map);
			
			return $res;

		}
	}

	/**
	*	立即购买-商品规格数量
	*   @param array $data  数据
	*/

	public function addNowsInfo($data)
	{
		if($data){
			$cate['tid'] = $data['tid'];
			$cate['gid'] = $data['gid'];
			$cate['iid'] = $data['iid'];
			$cate['ci_total'] = $data['i_price']*$data['i_number']*$data['ci_number'];
			$cate['ci_number'] = $data['ci_number'];
			Db::table('sz_cartinfo')->insert($cate);

			return true;

		}else{

			return false;
		}

	}

	/**
	*	立即购买的总价格
	*/
	public function addNowsTotal($tid)
	{
		if($tid){
			$t_total = Db::table('sz_cartinfo')->where('tid',$tid)->sum('ci_total');
				
			/* 把总价格加入到购物车表里面 */
			Db::table('sz_cart')->where('id',$tid)->update(['t_total'=>$t_total]);

			$result = Db::table('sz_cart')->where('id',$tid)->find();

			return $result;
		}
	}

	/**
	*	购物车列表
	*/
	public function getUserCart()
	{

		$user_id = Session::get('user_id');
		$result = Db::table('sz_cart')->alias('c')
				->join('sz_goods g','c.gid=g.id','left')
				->field('c.*,g.g_name,g.g_pic,g.g_description,g.g_unit')
				->where('c.uid',$user_id)
				->select();
		$res  = [];
		$arr  = [];
		$data = [];
		foreach($result as $key=>$val){
			$list[$key] = json_decode($val['g_pic'],true);
			foreach($list as $k1=>$v1){
				$res[$key]['photos'] = $v1['photos'];
				foreach($res[$key]['photos'] as $k2=>$v2){
					$arr['url'] = getPreviewUrl($v2['url']);
				}
			}

			$data[$key]['info'] = Db::table('sz_cartinfo')->alias('ci')
								->join('sz_info i','ci.iid=i.id','left')
								->field('ci.*,i.i_name,i.i_price,i.i_number,i.i_commission')
								->where('ci.tid',$val['id'])
								->select();

			$data[$key]['g_pic'] = $arr;
			$data[$key]['id'] = $val['id'];
			$data[$key]['g_name'] = $val['g_name'];
			$data[$key]['g_description'] = $val['g_description'];
			$data[$key]['g_unit'] = $val['g_unit'];
			$data[$key]['t_total'] = $val['t_total'];
			$data[$key]['gid'] = $val['gid'];

		}
		
		return $data;
	}

	/**
	*	购物车列表-商品规格信息
	*   @param string $tid  购物车id
	*/
	public function getCartInfo($tid)
	{
		if($tid){
			$result = Db::table('sz_cartinfo')->alias('ci')
					->join('sz_info i','ci.iid=i.id','left')
					->field('ci.*,i.i_name,i.i_price,i.i_number,i.i_commission')
					->where('ci.tid',$tid)
					->select();

			return $result;
		}else{
			return false;
		}
	}

	/**
	*	购物车里面的单条数据
	*   @param string $tid  购物车id
	*/
	public function getCartGoods($tid)
	{
		if($tid){
			$res = Db::table('sz_cart')->alias('c')
				 ->join('sz_goods g','c.gid=g.id','left')
				 ->field('c.*,g.g_name,g.g_pic')
				 ->where('id',$tid)
				 ->find();

			$list = json_decode($res['g_pic'],true);
			$arr = [];
			foreach($list['photos'] as $k1=>$v1){
				$arr['photos'][$k1]['url'] = getPreviewUrl($v1['url']);
			}

			$res['g_pic'] = json_encode($arr);

			return $res;
		}
	}

	/**
	*	ajax 添加购物车里某件商品的数量
	*	@param string $id 购物车商品信息表的id
	*	@param string $tid 单个购物车的id
	*/
	public function addCartInfoPost($id,$tid)
	{
		if($id){
			$res = Db::table('sz_cartinfo')->alias('ci')	
				 ->join('sz_info i','ci.iid=i.id','left')
				 ->field('ci.*,i.i_price,i.i_number,i.i_name')
				 ->where('ci.id',$id)->find();

			$res['ci_number'] +=1;

			$res['ci_total'] = $res['i_price']*$res['ci_number']*$res['i_number'];

			$row = [];

			/* 计算单个购物车的总金额 */
			$row['t_total'] +=$res['ci_total'];
			/* 改变单个购物车的总金额 */
			Db::table('sz_cart')->where('id',$tid)->update(['t_total'=>$row]);
			return $res;
		}

	}

	/**
	*	ajax 减少购物车里某件商品的数量
	*	@param string $id 购物车商品信息表的id
	*	@param string $tid 单个购物车的id
	*/
	public function deleteCartInfoPost($id,$tid)
	{
		if($id){
			$res = Db::table('sz_cartinfo')->alias('ci')	
				 ->join('sz_info i','ci.iid=i.id','left')
				 ->field('ci.*,i.i_price,i.i_number,i.i_name')
				 ->where('ci.id',$id)->find();

			$res['ci_number'] -=1;
			if($res['ci_number']<1){
				$res['ci_number'] =1;
			}

			$res['ci_total'] = $res['i_price']*$res['ci_number']*$res['i_number'];

			$row = [];

			/* 计算单个购物车的总金额 */
			$row['t_total'] -=$res['ci_total'];

			/* 改变单个购物车的总金额 */
			Db::table('sz_cart')->where('id',$tid)->update(['t_total'=>$row]);

			return $res;
		}

	}
	

	/**
	*   删除购物车
	*   @param string $id  购物车id
	*/
	public function deleteCart($id)
	{
		if($id){
			Db::table('sz_cart')->where('id',$id)->delete();

			Db::table('sz_cartinfo')->where('tid',$id)->delete();

			return true;

		}else{

			return false;
		}
	}

}